package com.yiwei.jmx.view;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/3/16 0016.
 */
public class RedRewardDialog extends Dialog {


    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.img_bg)
    ImageView imgBg;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.txt_content)
    TextView txtContent;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Context context;


    public RedRewardDialog(Context context) {
        super(context, R.style.mydialog);
        this.context = context;
        init();
    }

    public RedRewardDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public RedRewardDialog setOnSelect(OnSureListener listener) {
        selectListener = listener;
        return this;
    }

    @OnClick({R.id.btn_close, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
            case R.id.btn_submit:
                dismiss();
                break;
        }
    }

    public void setData(String money) {
        txtMoney.setText(money);
        txtContent.setText("恭喜您，获得现金红包" + money + "元");
    }

    public interface OnSureListener {
        void onSure(String id, String name);
    }

    private OnSureListener selectListener;

    private void init() {
        Window window = this.getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setWindowAnimations(R.style.mydialog_animation);
        setContentView(R.layout.dialog_red_reward);
        ButterKnife.bind(this);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = (int) (Util.getDisplayMetrics(context).heightPixels * 0.8);
        } else {
            lp.width = (int) (Util.getDisplayMetrics(context).widthPixels * 0.8);
        }
        window.setAttributes(lp);
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        this.setCanceledOnTouchOutside(false);

        txtMoney.post(new Runnable() {
            @Override
            public void run() {
                int height = imgBg.getHeight();
                int margin_top = (int) (height * 0.31);
                ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) txtMoney.getLayoutParams();
                lp.setMargins(0, margin_top, 0, 0);
                txtMoney.setLayoutParams(lp);
            }
        });
    }

}
